package com.ys.controller.api.purchase;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.purchase.TcgPurchaseAppointOrderProductComponent;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.purchase.TcgPurchaseAppointOrderService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

/*
 * 预约采购管理
 */
@Controller
@RequestMapping("/api/inter/tcgPurchaseAppointOrderManage")
public class ApiInterTcgPurchaseAppointOrderManageController {
	
	@Autowired
	private CommonApiService<TcgPurchaseAppointOrderProductComponent> commonApiService;
	@Autowired
	private TcgPurchaseAppointOrderService tcgPurchaseAppointOrderService;

	@RequiresPermissions("tcgPurchaseAppointOrderManage:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TcgPurchaseAppointOrderProductComponent cond,
			String conditions){
		
		cond = setSeachSql(cond, conditions);
		
		return commonApiService.searchLay(cond);
	}

	/**
	 * 删除
	 */
	@PostMapping("/cancel")
	@ResponseBody
	public R cancel(String bid){
		try {
			return tcgPurchaseAppointOrderService.cancel(bid);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}
	

	
	/*
	 * 导出
	 */
	@RequiresPermissions("tcgPurchaseAppointOrderManage:export")
	@RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TcgPurchaseAppointOrderProductComponent cond,
			String conditions) throws Exception{
		
		cond = setSeachSql(cond, conditions);
		
		cond.setOrderby("A.Id DESC");
		
		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.queryMap();
        
    	List<YsCol> listField = new YsCol()
    			.setMenuId(MenuId.TCG_PurchaseAppointOrder2)
    			.setFrontShow(YsColEnum.FrontShow.SHOW.value)
    			.setOrderby("A.SortNo asc")
    			.queryList();
    	
    	
        ExportExcelUtils<TcgPurchaseAppointOrderProductComponent> util = new ExportExcelUtils<TcgPurchaseAppointOrderProductComponent>(new TcgPurchaseAppointOrderProductComponent());
        
        String toFilePath = util.exportExcel(listData, listField, "预约采购管理");
        UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }

    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TcgPurchaseAppointOrderProductComponent setSeachSql(TcgPurchaseAppointOrderProductComponent cond, String conditions){
		cond.addLeftJoin("TCG_PurchaseAppointOrderItem B ON B.Id = A.Pid")
		.addField("B.ProductCode productCode")
		.addField("B.ProductName productName")
		.addField("B.CodeMerge codeMerge")
		.addField("B.ReserveNumber reserveNumber")
		
		.addLeftJoin("TCG_PurchaseAppointOrder C ON C.Bid = B.Pbid")
		.addField("C.Bid bid")
		.addField("C.Vgsbm vgsbm")

		.addField("C.CompanyVid companyVid")
		.addField("C.StateCancel stateCancel")

		.addLeftJoin("THR_OperateCompanyApply OCA ON OCA.Vgsbm = C.Vgsbm")//运营公司管理表
		.addField("OCA.Vgsjc vgsjc")

		.addLeftJoin("TB_CompanyInfo D ON D.Vid = C.CompanyVid")
		.addField("D.Vname companyVname")
		
		.addLeftJoin("YS_ProcInst F on F.Bid = C.Bid")
		.addField("F.UpdateTime auditTime")
		.where("C.EffectStock='1'");

		String _ys_auditTimeMin = ConditionUtil.getConditionValue(conditions, "_ys_auditTimeMin");
		String _ys_auditTimeMax = ConditionUtil.getConditionValue(conditions, "_ys_auditTimeMax");
		if (_ys_auditTimeMin != null && _ys_auditTimeMin.length() > 0) {
			cond.where("F.UpdateTime > '" + _ys_auditTimeMin + " 00:00:00'");
		}
		if (_ys_auditTimeMax != null && _ys_auditTimeMax.length() > 0) {
			cond.where("F.UpdateTime < '" + _ys_auditTimeMin + " 23:59:59'");
		}
		
        return  cond;
    }
}
